/**
 * Project Name:emm-website
 * File Name:BusinessChangeController.java
 * Package Name:com.emm.yixun.website.controller.marketing
 * Date:2017年11月13日下午1:37:22
 * Copyright (c) 2017, shihao.liu@qiaofang.info All Rights Reserved.
 *
*/

package com.emm.yixun.website.controller.marketing;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.model.BusinessChange;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.BusinessChangeDto;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.marketing.service.BusinessChangeServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
import com.emm.yixun.website.service.customer.SubscribeService;
import com.emm.yixun.website.utils.ExcelUtil;

/**
 * ClassName:BusinessChangeController <br/>
 * Date:     2017年11月13日 下午1:37:22 <br/>
 * @author   Administrator
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping(value = "/marketing/businessChange/")
public class BusinessChangeController extends BaseController<BusinessChange>{
	
	@Autowired
	private BusinessChangeServiceFacade businessChangeServiceFacade;
	
	@Autowired
	private SubscribeService subscribeService;
	/**
	 * 
	 * getModelByPage:(查询列表数据). <br/>
	 * @author Administrator
	 * @param businessChangeDto
	 * @param page
	 * @param limit
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "getModelByPage")
	public @ResponseBody Page<BusinessChange> getModelByPage(BusinessChangeDto businessChangeDto,Integer page, Integer limit){
		AjaxRes ar=getAjaxRes();
		try {
			if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:businessChange:list"))){
				User user = ShiroUtil.getCurrentUser();
				businessChangeDto.setMerchantId(user.getMerchantId());
				businessChangeDto.setPermissionUserId(user.getId());
				businessChangeDto.setExport(false);
				Page<BusinessChange> response = businessChangeServiceFacade.queryBusinessChangeInfo(businessChangeDto, PageUtil.begin(page, limit), limit);
				if(response.getSuccess()&&response.getRows()!=null){
					response.setPageNumber(page);
					return response;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("退换列表查询异常:",e);
		}
		return new Page<BusinessChange>();
	}
	/**
	 * 
	 * businessChangeExport:(退换列表导出). <br/>
	 * @author Administrator
	 * @param value
	 * @param response
	 * @param request
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "businessChangeExport")
	public void businessChangeExport(String value,HttpServletResponse response,HttpServletRequest request){
		AjaxRes ar=getAjaxRes();
		try {
			if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:businessChange:export"))){
				String jsonStr=URLDecoder.decode(value,"utf-8");
				BusinessChangeDto businessChangeDto = JSONObject.parseObject(jsonStr,BusinessChangeDto.class);
				User user = ShiroUtil.getCurrentUser();
				businessChangeDto.setMerchantId(user.getMerchantId());
				businessChangeDto.setPermissionUserId(user.getId());
				businessChangeDto.setExport(true);
				String[] name = new String[]{"退换信息"};
				List<String[]> headerList = new ArrayList<String[]>();
				List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>();
				String[] header = new String[]{
						"经办日前","退房日期","客户名称","客户手机","房号","户型","计价面积","合同金额","已收金额","退款金额","经纪人",
						"业务状态","审核状态"
						};
				headerList.add(header);
				Page<BusinessChange> responseList = businessChangeServiceFacade.queryBusinessChangeInfo(businessChangeDto, PageUtil.begin(1, 10), 10);
				List<BusinessChange> businessChangeList = responseList.getRows();
				List<Map<String, String>> listMap = subscribeService.businessChangeExport(businessChangeList, header);
				list.add(listMap);
				ExcelUtil.exportExcel(list, headerList, name, response);
			}
		} catch (Exception e) {
			logger.error("退换列表导出异常:",e);
		}
	}
	
}

